/*
 ============================================================================
 Name        : fonctions.h
 Author      : Antoine LAVAIL & Romain ROUFAST
 Version     : 1.0 (nucleus minimale)
 Copyright   :
 Description : D�clarations du n�cessaire pour l'�mulateur. Prototypes de fonctions.
 ============================================================================
 */


#ifndef DECLARATIONS_H_
#define DECLARATIONS_H_


// Codes-op�rateurs
#define ADD 0
#define SUB 1
#define JMP 2
#define JEQ 3
#define CALL 4
#define RET 5
#define PUSH 6
#define POP 7
#define LOAD 8
#define STORE 9
#define HALT 10

/*
 * Initialisation des modes d'adressage :
 * 		- registre-registre 	--> 0000
 * 		- registre-imm�diat 	--> 0100
 * 		- registre-direct 		--> 1000
 * 		- registre-indirect 	--> 1100
 */
#define REGREG 0
#define REGIMM 4
#define REGDIR 8
#define REGIND 12
#define DIRREG 1
#define DIRIMM 5
#define INDIMM 6
#define INDREG 2

/*
 * D�finir la composition d'un mot en m�moire :
 * 		- soit un short (brut)
 * 		- soit des champs de bits (codage)
 * La taille est de 16 bits
 */
typedef union {
	short brut;
	struct {
		unsigned codeop :6;
		unsigned mode :4;
		unsigned source :3;
		unsigned dest :3;
	} codage;
} mot;

#endif /* DECLARATIONS_H_ */

#ifndef EXEMPLE_UTILISATION_H_
#define EXEMPLE_UTILISATION_H_

// Taille de la m�moire centrale (instructions)
#define TAILLE_MEMI 52

// Exemple d'utilisation
static mot memi[TAILLE_MEMI] = {
		// ADD R1, #25
		{ .codage = {ADD,REGIMM,1} },
		{ .brut = 25},
		// ADD R1, R0
		{ .codage = {ADD,REGREG,1,0} },
		// ADD R2, [4]
		{ .codage = {ADD,REGDIR,2} },
		{ .brut = 4 },
		// ADD R5, [R6]
		{ .codage = {ADD, REGIND,6,5} },

		// JMP 16
		{ .codage = {JMP, REGIMM } },
		{ .brut = 16},

		// SUB R2, R1
		{ .codage = {SUB,REGREG,1,2} },
		// SUB R5, [6]
		{ .codage = {SUB,REGDIR,5} },
		{ .brut = 6 },
		// SUB R7, #1
		{ .codage = {SUB,REGIMM,7} },
		{ .brut = 1 },
		// SUB R0, [R3]
		{ .codage = {SUB, REGIND,3,0} },
		// RET
		{ .codage = {RET } },

		// LOAD R3, #10
		{ .codage = {LOAD,REGIMM,3} },
		{ .brut = 10 },
		// LOAD R4, [2]
		{ .codage = {LOAD,REGDIR,4} },
		{ .brut = 2 },
		// LOAD R3, [R2]
		{ .codage = {LOAD, REGIND,2,1} },

		// STORE [R1], #15
		{ .codage = {STORE,INDIMM,1} },
		{ .brut = 15},
		// STORE [2], #4
		{ .codage = {STORE,DIRIMM} },
		{ .brut = 2},
		{ .brut = 4},
		// STORE [7], R0;
		{ .codage = {STORE,DIRREG,0} },
		{ .brut = 7 },
		// STORE [R2], R3;
		{ .codage = {STORE,INDREG,3,2} },

		// PUSH R7
		{ .codage = {PUSH, REGREG,7} },
		// PUSH #20
		{ .codage = {PUSH,REGIMM} },
		{ .brut = 20 },
		// PUSH [3]
		{ .codage = {PUSH,REGDIR} },
		{ .brut = 3 },
		// PUSH [R1]
		{ .codage = {PUSH, REGIND, 1} },

		// POP R6
		{ .codage = {POP, REGREG, 1} },
		// POP [5]
		{ .codage = {POP, REGDIR} },
		{ .brut = 5 },
		// POP [R6]
		{ .codage = {POP, REGIND, 6} },

		// CALL 9
		{ .codage = {CALL, REGIMM } },
		{ .brut = 9 },

		// SUB R5, #1
		{ .codage = {SUB, REGIMM,5} },
		{ .brut = 1 },
		// JEQ
		{ .codage = {JEQ, REGIMM } },
		{ .brut = 44 },

		// HALT
		{ .codage = {HALT } },


};


#endif /* EXEMPLE_UTILISATION_H_ */

#ifndef FONCTIONS_H_
#define FONCTIONS_H_

void add(short* reg,short* memd, short* PC, short* SR);
void sub(short* reg, short* memd, short* PC, short* SR);
void load(short* reg, short* memd, short* PC);
void store(short* reg, short* memd, short* PC);
void push(short* reg, short* memd, short* pilE, short* PC, short* SP);
void pop(short* reg, short* memd, short* pilE, short* PC, short* SP);

void etatDesRegistres(short* reg);
void etatDeLaPile(short* pilE);
char* codeToRegistre(short code);
void waitForEnter();

#endif /* FONCTIONS_H_ */
